import axios from 'axios'
import type { UserLogin, ReUserLogin, User } from '@/types/userType'
import type { ReEquipmentQuery, ReEquipmentAdd, Equipment } from '@/types/EquipmentType'
import type { BriefProject, CreateProject, Project } from '@/types/ProjectType'
import type { Category, ParaStandard, Standard } from '@/types/StandardType'
import type { BriefParameter, Parameter } from '@/types/ParameterType'
import type { _standard } from '@/stores/AllYouNeedIsStore'

export const api_url = '/api/'

export const api = {
  login: async function (x: UserLogin): Promise<ReUserLogin> {
    let result: ReUserLogin = { success: false }
    await axios
      .post(
        api_url + 'login',
        {
          username: x.username,
          pwd: x.pwd
        },
        {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        }
      )
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  //设备相关接口
  queryEquipment: async function (_page: number, _per_page_num: number): Promise<ReEquipmentQuery> {
    let result: ReEquipmentQuery = {
      content: [],
      totalPages: 0,
      first: true,
      last: true
    }
    await axios
      .get(api_url + 'equipment/get', {
        params: {
          page: _page,
          per_page_num: _per_page_num
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },

  addEquipment: async function (x: Equipment): Promise<ReEquipmentAdd> {
    let result: ReEquipmentAdd = {
      success: false
    }
    await axios
      .post(api_url + 'equipment/add', x, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },

  getAllEquipment: async function (): Promise<Equipment[]> {
    let result: Equipment[] = []
    await axios.get(api_url + 'equipment/getall').then((resp) => {
      result = resp.data
    })
    return result
  },
  addEquipmentByFile: async function (x: any): Promise<string> {
    let result: string = ''
    await axios
      .post(api_url + 'equipment/file', x, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  //Category相关接口
  getAllCategory: async function (): Promise<Category[]> {
    let result: Category[] = []
    await axios.get(api_url + 'category').then((resp) => {
      result = resp.data
    })
    return result
  },
  //Project相关接口
  addProject: async function (x: CreateProject): Promise<Project> {
    let result: Project = {
      id: 0,
      name: '',
      reason: '',
      status: '',
      labAdmin: {
        username: '',
        type: ''
      },
      standards: []
    }
    await axios
      .post(api_url + 'project', x, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  getProcessingAndCompletedProject: async function (): Promise<BriefProject[]> {
    let result: BriefProject[] = []
    await axios.get(api_url + 'project/progressing-completed').then((resp) => {
      result = resp.data
    })
    return result
  },
  getConfirmingProject: async function (): Promise<BriefProject[]> {
    let result: BriefProject[] = []
    await axios.get(api_url + 'project/confirming').then((resp) => {
      result = resp.data
    })
    return result
  },
  getConfirmingAndConfirmedAndAgreedProject: async function (): Promise<BriefProject[]> {
    let result: BriefProject[] = []
    await axios.get(api_url + 'project/confirming-confirmed-agreed').then((resp) => {
      result = resp.data
    })
    return result
  },
  getConfirmedAndAgreedProject: async function (): Promise<BriefProject[]> {
    let result: BriefProject[] = []
    await axios.get(api_url + 'project/confirmed-agreed').then((resp) => {
      result = resp.data
    })
    return result
  },
  getProjectById: async function (_id: number): Promise<Project> {
    let result: Project = {
      id: 0,
      name: '',
      reason: '',
      status: '',
      labAdmin: {
        username: '',
        type: ''
      },
      standards: []
    }
    await axios
      .get(api_url + 'project/get', {
        params: {
          id: _id
        }
      })
      .then((resp) => {
        result = resp.data
        result.standards.forEach((s) => {
          if (s.sample == undefined) {
            s.sample = { informationFile: '', photo: '', followStandard: true }
          }
          if (s.simulation == undefined) {
            s.simulation = { report: '', origin: '', followStandard: true }
          }
          if (s.sop == undefined) {
            s.sop = { file: '', followStandard: true }
          }
          s.parameterList.forEach((p) => {
            if (p.sample == undefined) {
              p.sample = { informationFile: '', photo: '', followStandard: true }
            }
            if (p.simulation == undefined) {
              p.simulation = { report: '', origin: '', followStandard: true }
            }
            if (p.sop == undefined) {
              p.sop = { file: '', followStandard: true }
            }
          })
        })
      })
    return result
  },
  toQualityDept: async function (x: number): Promise<undefined> {
    await axios.get(api_url + 'project/to-quality', {
      params: {
        id: x
      }
    })
  },
  confirm: async function (x: number): Promise<undefined> {
    await axios.get(api_url + 'project/to-confirm', {
      params: {
        id: x
      }
    })
  },
  sendBackToCompleted: async function (x: number): Promise<undefined> {
    await axios.get(api_url + 'project/to-completed', {
      params: {
        id: x
      }
    })
  },
  agreed: async function (x: number): Promise<undefined> {
    await axios.get(api_url + 'project/to-agreed', {
      params: {
        id: x
      }
    })
  },
  //标准相关接口
  createStandard: async function (x: Standard): Promise<number> {
    let result: number = -1
    await axios
      .post(api_url + 'standard', x, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  getParaStandard: async function (x: number): Promise<ParaStandard> {
    let result: ParaStandard = {
      id: 0,
      sample: {
        informationFile: '',
        photo: '',
        followStandard: true
      },
      sop: {
        file: '',
        followStandard: true
      },
      simulation: {
        report: '',
        origin: '',
        followStandard: false
      }
    }
    await axios.get(api_url + 'standard/getpara', { params: { id: x } }).then((resp) => {
      result = resp.data
      if (result.sample == undefined) {
        result.sample = {
          informationFile: '',
          photo: '',
          followStandard: true
        }
      }
      if (result.sop == undefined) {
        result.sop = { file: '', followStandard: true }
      }
      if (result.simulation == undefined) {
        result.simulation = { report: '', origin: '', followStandard: true }
      }
    })
    return result
  },
  //参数相关接口
  getParameterByStandardId: async function (x: number): Promise<Parameter[]> {
    let result: Parameter[] = []
    await axios.get(api_url + 'parameter/getbystand', { params: { id: x } }).then((resp) => {
      result = resp.data
    })
    return result
  },
  createParameterUnderStandard: async function (x: _standard[]): Promise<Project> {
    let result: Project = {
      id: 0,
      name: '',
      reason: '',
      status: '',
      labAdmin: undefined,
      standards: []
    }
    await axios
      .post(api_url + 'parameter/cpus', x, {
        headers: {
          'Content-Type': 'application/json'
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  getUncompleteBriefParameterByEmpId: async function (x: number): Promise<BriefParameter[]> {
    let result: BriefParameter[] = []
    await axios
      .get(api_url + 'parameter/getUncompletedbyemp', { params: { id: x } })
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  getCompletedBriefParameterByEmpId: async function (x: number): Promise<BriefParameter[]> {
    let result: BriefParameter[] = []
    await axios.get(api_url + 'parameter/getCompletedbyemp', { params: { id: x } }).then((resp) => {
      result = resp.data
    })
    return result
  },
  getParameterById: async function (x: number): Promise<Parameter> {
    let result: Parameter = {
      name: '',
      emps: [],
      humanList: [],
      equipmentList: [],
      sample: {
        informationFile: '',
        photo: '',
        followStandard: true
      },
      sop: {
        file: '',
        followStandard: true
      },
      facilityList: [],
      compareValidList: [],
      simulation: {
        report: '',
        origin: '',
        followStandard: true
      },
      status: 'Uncompleted',
      labAdmin: {
        username: '',
        type: ''
      }
    }
    await axios.get(api_url + 'parameter/get', { params: { id: x } }).then((resp) => {
      result = resp.data
      if (result.sample == undefined) {
        result.sample = {
          informationFile: '',
          photo: '',
          followStandard: true
        }
      }
      if (result.sop == undefined) {
        result.sop = { file: '', followStandard: true }
      }
      if (result.simulation == undefined) {
        result.simulation = { report: '', origin: '', followStandard: true }
      }
    })
    return result
  },
  submitParameter: async function (
    parameter: Parameter,
    standard: ParaStandard
  ): Promise<Parameter> {
    let result: Parameter = {
      name: '',
      emps: [],
      humanList: [],
      equipmentList: [],
      sample: {
        informationFile: '',
        photo: '',
        followStandard: true
      },
      sop: {
        file: '',
        followStandard: true
      },
      facilityList: [],
      compareValidList: [],
      simulation: {
        report: '',
        origin: '',
        followStandard: true
      },
      status: 'Uncompleted',
      labAdmin: {
        username: '',
        type: ''
      }
    }
    await axios
      .post(
        api_url + 'parameter/submit',
        { parameter: parameter, standardPara: standard },
        {
          headers: {
            'Content-Type': 'application/json'
          }
        }
      )
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  changeStatus: async function (x: number | undefined): Promise<undefined> {
    await axios.get(api_url + 'parameter/change-status', {
      params: {
        id: x
      }
    })
  },
  sendBack: async function (x: number, y: number): Promise<undefined> {
    await axios.get(api_url + 'parameter/sendback', {
      params: {
        id: x,
        project: y
      }
    })
  },
  //文件操作
  upload: async function (x: any): Promise<string> {
    let result: string = ''
    await axios
      .post(api_url + 'file', x, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  },
  getAllEmps: async function (): Promise<User[]> {
    let result: User[] = []
    await axios.get(api_url + 'user/getall').then((resp) => {
      result = resp.data
    })
    return result
  },
  getExcel: async function (x: number): Promise<string[]> {
    let result: string[] = []
    await axios
      .get(api_url + 'file/generate', {
        params: {
          id: x
        }
      })
      .then((resp) => {
        result = resp.data
      })
    return result
  }
}
